#include <errno.h>
#include <fcntl.h>
#include "xfiforeadthread.h"
#include <xwritelogexport.h>
#include <string.h>
#include <stdio.h>
#include <QTime>

XFifoReadThread::XFifoReadThread(QObject *parent) 
{
	m_fdOut = open("/tmp/cplayer_out",O_RDONLY,O_NONBLOCK);
	if (-1==m_fdOut) {
		LOG_E("error:%s with errno: %d\n",strerror(errno),m_fdOut);
	} else {
		LOG_E("Open Out FIFO \t\t...[OK]\n");
	} 
}

XFifoReadThread::~XFifoReadThread()
{
	close(m_fdOut);
}
void XFifoReadThread::run()
{
	LOG_E("Run Thread");
	while (1) {
		memset(m_result,0,32);
		read(m_fdOut, m_result, 32);
		QString result = QString(m_result);
		result.chop(1);
		
//		LOG_E("FIFO:[%s]", result.toStdString().c_str());
//		QTime current = QTime::currentTime();
//		QString time = current.toString("hh:mm:ss.zzz");
//		LOG_E("time:%s", time.toStdString().c_str());
		
		if(0==QString::compare(result, "pLaYeNd")) {
			LOG_E("Emit Signal: PlayEnd");
			emit sgPlayEnd();  //notify XMoviePlayer,
		} else if (0==QString::compare(result, "sToPeNd")) {
			LOG_E("Emit Signal: StopEnd");
			emit sgStopEnd();
		} else {
			LOG_E("[Fatal Error]Unknown value");
		}
	}
	exec();
}
